﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmsVotes.DataAccess
{
    public class StateManager : BaseManager
    {
        /// <summary>
        /// Возвращает голосование по ID
        /// </summary>
        /// <param name="stateID">Идентификатор голосования</param>
        /// <returns></returns>
        public static VoteState GetVoteStateByID(int stateID)
        {
            var state = (from item in EFContext.VoteState.Include("CurrentQuestion.Chart")
                         where item.ID == stateID
                         select item).FirstOrDefault();
            return state;
        }

        /// <summary>
        /// Метод для тестирования.
        /// Добавляет случайные числа в качестве результатов голосования
        /// </summary>
        public static void AddVotesToState(int stateID)
        {
            var voteState = GetVoteStateByID(stateID);
            voteState.AnswerStates.Load();
            var random = new Random();
            foreach (var answer in voteState.CurrentQuestion.Answers)
            {
                var answerState = (from state in voteState.AnswerStates
                                   where (state.Answer != null) && (state.Answer.ID == answer.ID)
                                   select state).FirstOrDefault();
                if (answerState == null)
                {
                    answerState = new AnswerState();
                    answerState.Answer = answer;
                    answerState.VoteState = voteState;
                    answerState.Votes = 0;
                    EFContext.AddToAnswerState(answerState);
                }
                answerState.Votes += (int)(random.NextDouble() * 100);
            }
            Save();
        }

        /// <summary>
        /// Список всех голосований
        /// </summary>
        /// <returns></returns>
        public static List<VoteState> GetAll(string login)
        {
            var voteStates = (from voteState in EFContext.VoteState
                              where voteState.Vote.OwnerLogin == login
                              select voteState);
            return voteStates.ToList();

        }

        /// <summary>
        /// Возвращает общее количество голосов для голосования
        /// </summary>
        /// <param name="state">Идентификатор голосования</param>
        /// <returns>Количество голосов</returns>
        public static int TotalVotes(VoteState state)
        {
            int total = 0;
            state.AnswerStates.SmartLoad(state);
            foreach (var answerState in state.AnswerStates)
                total += answerState.Votes;
            return total;
        }

        /// <summary>
        /// Возвращает количество голосов, отданных за вопрос
        /// </summary>
        /// <param name="answer">Ответ</param>
        /// <param name="stateID">Идентификатор голосования</param>
        /// <returns>Количество проголосовавших</returns>
        public static int AnswerVotes(Answer answer, int stateID)
        {
            return (from item in answer.AnswerStates
                    where item.VoteState.ID == stateID
                    select item.Votes).FirstOrDefault();
        }
    }
}
